import 'base_list_controller.dart';

class BaseListMoreController extends BaseListController {
  int pageNum = 1;
  int pageSize = 20;

  bool get canLoadMore => pageNum * pageSize <= (list?.length ?? 0) || (pageNum > 1 && list?.isNotEmpty == true);

  @override
  bool get emptyViewRefresher => true;

  @override
  Future<bool> refreshList() {
    pageNum = 1;
    refreshController.resetNoData();
    return super.refreshList();
  }

  Future<bool> loadMoreList() {
    pageNum++;
    return loadData().then(
      (value) {
        if (value.isNotEmpty) {
          list?.addAll(value);
          refreshController.loadComplete();
        } else {
          refreshController.loadNoData();
        }
        doFinished();
        return true;
      },
      onError: (error) {
        if (list?.isNotEmpty == true) {
          doFinished();
          refreshController.loadNoData();
        } else {
          doError(error);
          refreshController.loadFailed();
        }
        return true;
      },
    );
  }
}
